package com.hutao.subject.common.entity;

import lombok.Data;
import lombok.Setter;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Data
public class PageResult<T> implements Serializable {

    @Setter
    private Integer pageNo = 1;
    @Setter
    private Integer pageSize = 20;
    private Integer total = 0;
    private Integer totalPage = 1;
    private List<T> result = Collections.emptyList();
    private Integer start = 1;
    private Integer end = 0;

    public void setRecords(List<T> result) {
        this.result = result;
        if(result != null && !result.isEmpty()) {
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total) {
        this.total = total;
        if(pageSize > 0){
            this.totalPage = total % this.pageSize == 0 ? total / this.pageSize : total / this.pageSize + 1;
        }else {
            this.totalPage = 0;
        }
        this.start = (this.pageSize > 0 ? this.pageSize * (this.pageNo - 1) : 0) + 1;
        this.end = (this.start - 1 + this.pageSize * (this.pageSize > 0 ? 1 : 0));
    }

}
